#include "user_dao.h"
#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
#include "common.h"
#include "mysql_helper.h"
#include "xml.h"

int
dao_login_check(const char *name, const char *password)
{
    char sql[4096];
    snprintf(sql, sizeof(sql), "select * from user where name='%s' and password='%s'", name, password);
	MYSQL_RES *res = NULL; // mysql 记录集
    if ((res = findsql(sql)) == NULL)
    {
        return (-1);
    }
    
    int result = 0;
    result = mysql_num_rows(res);
    mysql_free_result(res); // 释放结果集
    return (result != 0 ? 0 : -1);
}

int
dao_check_user_is_exists(const char *name)
{
    char sql[4096];
    snprintf(sql, sizeof(sql), "select * from user where name='%s'", name);
	MYSQL_RES *res = NULL; // mysql 记录集
    if ((res = findsql(sql)) == NULL)
    {
        return (-1);
    }
    
    int result = 0;
    result = mysql_num_rows(res);
    mysql_free_result(res); // 释放结果集
    return (result != 0 ? 0 : -1);
}


int
dao_add_user(struct user *u)
{
    char sql[4096];
    snprintf(sql, sizeof(sql), "insert into `user`(name, password, telephone, age, gender) \
         values('%s', '%s', '%s', %d, %d)", u->name, u->password, u->telephone, u->age, u->gender);
    return (insertsql(sql));
}
